<template>
	<view>
		<tn-nav-bar :isBack='true' :backTitle='" "' bottomShadow fixed alpha customBack>
			<view slot="back" class='customBack' @click="goBack">
				<image src="/static/product/back.png" class="back" mode=""></image>
			</view>
			<view slot="right" @click="collect" v-if="type==2">
				<image src="/static/product/scth.png" v-if="item.isCollect" class="shoucang" mode=""></image>
				<image src="/static/product/sc.png" v-else class="shoucang" mode=""></image>
			</view>
			<view class="" style="color: #fff;">
				详情
			</view>
		</tn-nav-bar>
		<loading v-if="loading"></loading>
		<view class="" v-else>
			<!-- 轮播图片 -->
			<u-swiper :list="list" imgMode='aspectFill' keyName="image" height='777rpx' :autoplay="false" circular></u-swiper>
			<!-- 详情 -->
			<view class="xq">
				<view class="pn">
					<view class="tn-flex tn-flex-col-bottom">
						<view class="pn__price">
							<text style="font-size: 32rpx;">￥</text>{{item.price}}
						</view>
						<view class="pn__oldprice">
							￥{{item.oldprice}}
						</view>
					</view>
					<view class="pn__name">
						{{item.name}}
					</view>
				</view>
				<view class="addr" @click="clickaddr">
					<view class="tn-flex">
						地址
						<view class="line">

						</view>
						<view class="addr__text">
							{{addr?addr:'请填写收货地址'}}

						</view>
					</view>
					<view class="tn-icon-right">

					</view>
				</view>
				<view class="tn-flex tn-flex-row-between tn-flex-row-center">
					<view class="kc">
						库存{{item.num}}
					</view>
					<view class="xl">
						销量:
						<text style="color: #45C47B;font-weight: bold;font-size: 28rpx;">{{item.salenum}}</text>
					</view>
				</view>
			</view>
			<view class="grod">

			</view>
			<!-- 详情信息 -->
			<view class="content">
				<view class="content__title">
					<image src="/static/product/title.png" class="content__title__icon" mode=""></image>
				</view>
				<!-- 富文本 -->
				<view class="" style="padding: 10rpx 20rpx;">
					<mp-html :content="item.desc" />
				</view>
			</view>
			<view class="footer">
				<view class="footer__sc" @click="collect">
					<image src="/static/product/sc.png" v-if="!item.isCollect" class="footer__sc__icon" mode=""></image>
					<image src="/static/product/scth.png" v-else class="footer__sc__icon" mode=""></image>
					收藏
				</view>
				<image @click="submit" src="/static/product/buy.png" class="footer__buy" mode=""></image>
			</view>
		</view>
		<button class="kef u-reset-button" open-type="contact">
			<image src="/static/product/kf.png" class="keficon" mode=""></image>
		</button>
		<view class="desc_image_box" v-if="item.desc_image" @click="clickvd">
			<video :src="URL(item.desc_image)" :muted='true' :controls='false' autoplay class="desc_image"></video>
			<view class="desc_image_text">
				商品讲解
			</view>
		</view>
	</view>
</template>

<script>
	import product from '@/api/_product.js'
	import home from '@/api/_home.js'
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				list: [],
				item: {},
				imgs: [],
				type: 2,
				form: {
					type: '',
					info_id: '',
					price: '',
					start_time: '',
					name: "",
					image: "",
					address_id: '',
					num: 1
				},
				addr: '',
				loading:true
			};
		},
		onLoad(o) {
			this.type = o.type
			if (o.type == 2) {
				product.getGoodsInfo({
					id: o.id
				}).then(res => {
					if (res.data.code == 1) {
						this.item = res.data.data
						this.item.images.split(',').filter(Boolean).map(t => {
							this.list.push({
								image: this.URL(t),
								fm:this.URL(this.item.image)
							})
							this.imgs.push(this.URL(t))
						})
						
						this.loading=false
					}
				})
			}
			if (o.type == 3) {
				product.getCardInfo({
					id: o.id
				}).then(res => {
					if (res.data.code == 1) {
						this.item = res.data.data
						this.item.images.split(',').filter(Boolean).map(t => {
							this.list.push({
								image: this.URL(t),
								fm:this.URL(this.item.image)
							})
							this.imgs.push(this.URL(t))
						})
						this.loading=false
					}
				})
			}

		},
		methods: {
			clickswp(index) {
				this.preview(this.imgs, index)
			},
			collect() {
				home.infoCollect({
					type: 3,
					info_id: this.item.id
				}).then(res => {
					if (res.data.code == 1) {
						this.item.isCollect = !this.item.isCollect
						// this.getuser()
					}
				})
			},
			clickvd(){
				this.$tn.vuex('videourl', this.URL(this.item.desc_image))
				this.to('/pages/achievement/video')
			},
			submit() {
				let {
					address_id
				} = this.form
				if (!address_id) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none'
					})
					return
				}
				if (this.type == 2) {
					this.form.info_id = this.item.id
					this.form.price = this.item.price
					this.form.name = this.item.name
					this.form.image = this.item.image
					this.form.type = this.type


					home.undifinedOrder(this.form).then(res => {
						if (res.data.code == 1) {
							this.paybuy(res)
						}
					})
				}
				if (this.type == 3) {
					product.buyCard({
						card_id: this.item.id,
						price: this.item.price,
						name: this.item.name,
						image: this.item.images,
						address_id: this.form.address_id
					}).then(res => {
						if (res.data.code == 1) {
							this.paybuy(res)
						}

					})
				}

			},
			paybuy(res) {
				let _this = this
				let data = res.data.data
				let appId = data.appId;
				let timeStamp = data.timeStamp;
				let nonceStr = data.nonceStr;
				let packageX = data.package;
				let signType = 'MD5';
				let paySign = data.paySign;

				uni.requestPayment({
					provider: 'wxpay',
					appId: appId, //小程序Appid
					timeStamp: timeStamp, //创建订单时间戳
					nonceStr: nonceStr,
					package: packageX, // 订单包
					signType: signType, // 加密方式统一'MD5'
					paySign: paySign, // 后台支付签名返回
					success: function(res) {
						_this.$tn.message.toast('支付成功')
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/product/manage'
							})
						}, 1000)
					},
					fail: function(err) {
						_this.$tn.message.toast('支付失败')
					},
					complete() {

					}
				});

			},
			clickaddr() {
				if (this.addr) {
					this.to('/pages/my/addrlist')
				} else {
					this.to('/pages/my/addr')
				}
			}
		},
		onShow() {
			// 获取默认地址
			user.defaultAddress().then(res => {
				if (res.data.code == 1) {
					let data = res.data.data
					if (data) {
						this.addr = data.province + data.city + data.area
						this.form.address_id = data.id
					}

				}
			})
		}
	}
</script>

<style lang="scss">
	.shoucang {
		width: 38rpx;
		height: 38rpx;
	}

	.back {
		width: 19rpx;
		height: 32rpx;
		margin-left: 10rpx;
	}

	.customBack {
		height: 100%;
		display: flex;
		align-items: center;
	}

	.xq {
		padding-left: 30rpx;
		padding-bottom: 49rpx;
	}

	.pn {
		padding: 26rpx 28rpx 40rpx 0;
		border-bottom: 2rpx solid #F7F7F9;
		display: flex;
		align-items: center;
		justify-content: space-between;

		&__price {
			font-family: DIN Next LT Pro;
			font-weight: bold;
			font-size: 43rpx;
			color: #FF4D30;
			line-height: 31rpx;
		}

		&__oldprice {
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 23rpx;
			color: #919191;
			line-height: 23rpx;
			text-decoration-line: line-through;
			margin-left: 8rpx;
		}

		&__name {
			font-family: PingFang SC;
			font-weight: 800;
			font-size: 33rpx;
			color: #181818;
			line-height: 33rpx;
			margin-top: 11rpx;
		}
	}

	.addr {
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 30rpx;
		color: #949494;
		line-height: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 20rpx 53rpx 0;

		.line {
			width: 3rpx;
			height: 23rpx;
			background: #EBEAE9;
			margin: 0 22rpx;
		}

		&__text {
			font-family: PingFang SC;
			font-weight: bold;
			color: #222222;
		}
	}

	.kc {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #4C4C4C;
		line-height: 28rpx;
	}

	.xl {
		padding-right: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #4C4C4C;
		display: flex;
		align-items: center;
		line-height: 28rpx;
	}

	.grod {
		width: 750rpx;
		height: 22rpx;
		background: #F7F7F9;
	}

	.footer {
		width: 750rpx;
		height: 186rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;

		&__sc {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			color: #151515;
			line-height: 30rpx;
			justify-content: center;
			margin-right: 18rpx;

			&__icon {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: 14rpx;
			}

		}

		&__buy {
			width: 616rpx;
			height: 131rpx;
		}
	}

	.kef {
		width: 111rpx;
		height: 115rpx;
		position: fixed;
		bottom: 235rpx;
		right: 15rpx;
	}

	.content {
		background-color: #f6f6f6;

		padding-bottom: 200rpx;

		&__title {
			width: 750rpx;
			padding: 35rpx 0 15rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;

			&__icon {
				width: 350rpx;
				height: 48rpx;
			}
		}
	}
	.desc_image{
		width: 150rpx;
		height: 200rpx;
		
	}
	.desc_image_box{
		border-radius: 10rpx;
		overflow: hidden;
		position: fixed;
		top: 480rpx;
		right: 30rpx;
		background-color: #FF4D30;
	}
	.desc_image_text{
		text-align: center;
		color: #fff;
		padding-bottom: 3px;
		font-size: 24rpx;
	}
</style>